<%--
  Created by IntelliJ IDEA.
  User: ZXW
  Date: 2022/4/26
  Time: 16:30
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>个人主页</title>




  <!-- 静态包含base标签 jQuery文件 -->
  <%@include file="/pages/common/head.jsp"%>
  <link rel="stylesheet" href="static/css/common.css">
  <link rel="stylesheet" href="static/css/blog_list.css">
<script type="text/javascript">
  $(function (){
      var queryIsFreezedUser = document.getElementById("queryIsFreezedUser");

      var report = document.getElementById("report");
      var receiveReport = document.getElementById("receiveReport");


      var updateBlog = document.getElementById("updateBlog");
      var deleteBlog = document.getElementById("deleteBlog");

      var noRelation = document.getElementById("noRelation");
      var relation = document.getElementById("relation");

      //如果当前用户是管理员，则他可以查看冻结用户并进行相关操作
      $.getJSON("http://localhost:8080/CSDN/userServlet",
          "action=isAdmin&userName=${sessionScope.user.username}",
          function (data){




              console.log(data);
              if(data.admin==true) {
                  //说明当前用户是管理员，就有查询冻结用户功能
                  queryIsFreezedUser.style.display="block";
                  //说明当前用户是管理员，就没有举报功能
                    report.style.display="none";
                  //说明当前用户是管理员，有查看举报内容的功能
                  receiveReport.style.display="block";

                  $("a#deleteBlog").show();
                  $("a#updateBlog").show();

                  // //说明当前用户是管理员，有修改和删除用户文章的功能
                  updateBlog.style.display="block";
                  deleteBlog.style.display="block";
                    //说明此时当前登录的用户和正在查看的用户不是同一个
                  <c:if test="${not empty sessionScope.queryUserName}">

                  //用于管理员判断当前用户的是否处于冻结状态
                  $.getJSON("http://localhost:8080/CSDN/userServlet",
                      "action=isFreezed&currentUserName=${sessionScope.queryUserName}",
                      function (data){

                          // console.log(data);
                          if(data.isFreezed==true) {
                              //说明当前用户被冻结

                              Freezed.style.display="none";
                              isFreezed.style.display="block";


                          }else {
                              //说明当前用户没有被冻结

                              Freezed.style.display="block";
                              isFreezed.style.display="none";

                          }

                      });
                  </c:if>



              }else {
                  //说明当前用户不是管理员，就没有查询冻结用户功能
                  queryIsFreezedUser.style.display="none";

                  //说明当前用户不是管理员，就有举报功能
                  report.style.display="block";
                  //说明当前用户不是管理员，没有查看举报内容的功能
                  receiveReport.style.display="none";
              }
          });
      var isFreezed = document.getElementById("isFreezed");
      var Freezed = document.getElementById("Freezed");

        //点击冻结用户
      $("#Freezed").click(function (){

          Freezed.style.display="none";
          isFreezed.style.display="block";

          $.getJSON("http://localhost:8080/CSDN/userServlet","action=setIsFreezed&currentUserName=${sessionScope.queryUserName}",function (data){});


      });





      //说明此时当前登录的用户和正在查看的用户不是同一个
      <c:if test="${not empty sessionScope.queryUserName}">
      $.getJSON("http://localhost:8080/CSDN/relationServlet",
          "action=isFollower&followerName=${sessionScope.queryUserName}&followedName=${sessionScope.user.username}",
          function (data){

        // console.log(data);
          if(data.follower==true) {
              //说明当前用户没有关注正在查看的用户
              noRelation.style.display="none";
              relation.style.display="block";

          }else {
              //说明当前用户有关注正在查看的用户
              noRelation.style.display="block";
              relation.style.display="none";
          }

      });



      </c:if>

      $("#relation").click(function (){
          noRelation.style.display="block";
          relation.style.display="none";
          //保存关注数据
          $.getJSON("http://localhost:8080/CSDN/relationServlet","action=addFollower&followerName=${sessionScope.queryUserName}&followedName=${sessionScope.user.username}",function (data){});

          <%--$.getJSON("http://localhost:8080/CSDN/user/favourServlet","action=queryFavourByBlogId&blogId=${sessionScope.blog.id}",function (data){});--%>
          <%--$("span.favourMsg").text( ${sessionScope.favourNum});--%>

      });
      $("#noRelation").click(function (){

          noRelation.style.display="none";
          relation.style.display="block";
          //保存取消关注数据
          $.getJSON("http://localhost:8080/CSDN/relationServlet","action=deleteFollower&followerName=${sessionScope.queryUserName}&followedName=${sessionScope.user.username}",function (data){});

          <%--$.getJSON("http://localhost:8080/CSDN/user/favourServlet","action=queryFavourByBlogId&blogId=${sessionScope.blog.id}",function (data){});--%>
          <%--$("span.favourMsg").text( ${sessionScope.favourNum});--%>
      });


          // 给 【删除】绑定单击事件
           $("a.deleteBlog").click(function (){
           return confirm("你确定删除这篇文章吗？");
  });

           $("a.showBlog").click(function (){
             console.log(${sessionScope.blog.id});
             $.getJSON("http://localhost:8080/CSDN/user/favourServlet","action=queryFavourByBlogId&blogId=${sessionScope.blog.id}",function (data){});

           });
  });

</script>


</head>
<body>
<!-- 导航栏 -->
<div class="nav">
  <img src="image/log.png" alt="">
  <span class="title">个人主页</span>
  <!-- 使用 span 把左右两侧的元素给撑开 -->
  <span class="spacer"></span>
  <a href="index.jsp">主页</a>
   <a href="userServlet?action=queryIsFreezedUser"> <input id="queryIsFreezedUser" style="display: none;" type="button" value="查看冻结用户"/></a>
  <a href="pages/blog/blog_edit.jsp?pageNo=${sessionScope.page.pageTotal}">写博客</a>

    <a href="pages/report/report_edit.jsp"> <input id="report" style="display: none;" type="button" value="举报"/></a>

    <a href="reportServlet?action=queryReport"> <input id="receiveReport" style="display: none;" type="button" value="查看举报内容"/></a>





    <a href="draftServlet?action=queryDraft">草稿箱</a>
  <a href="user/collectionServlet?action=queryCollection&userId=${sessionScope.user.id}">收藏夹</a>
  <a href="pages/user/updateUserMessage.jsp">修改个人信息</a>
  <a href="userServlet?action=logout">注销</a>
</div>
<!-- 版心 -->
<div class="container">
  <!-- 左侧区域，显示用户信息 -->
  <div class="container-left">
    <!-- 用户详情 -->
    <div class="card">
      <!-- 用户的头像 -->
      <img src="image/head.jpg" alt="">
      <!-- 用户名 -->
      <h3 name="username">
<%--        ${sessionScope.queryUserName==null?"${sessionScope.queryUserName}":"${sessionScope.user.username}"}--%>
        <c:if test="${not empty sessionScope.queryUserName}">${sessionScope.queryUserName}</c:if>
        <c:if test="${empty sessionScope.queryUserName}">${sessionScope.user.username}</c:if>
<%--    <c:if test="${sessionScope.self==false && sessionScope.admin==true}">${sessionScope.queryUserName}</c:if>--%>
<%--    <c:if test="${sessionScope.self==true}">${sessionScope.user.username}</c:if>--%>
      </h3>
      <input type="hidden" name="userId" value="${sessionScope.user.id}">

      <input type="button" style="display:none;" id="relation" name="relation" value="关注">
      <input type="button" style="display:none;"  id="noRelation" name="noRelation" value="取消关注">

        <input type="button" style="display:none;" id="Freezed" name="Freezed" value="冻结用户">
      <input type="button" style="display:none;"  id="isFreezed" name="isFreezed" value="该用户已冻结">

      <div class="counter">

        <span href="#"><a class="follower"
      href="relationServlet?action=queryFollower&username=<c:if test="${not empty sessionScope.queryUserName}">${sessionScope.queryUserName}</c:if><c:if test="${empty sessionScope.queryUserName}">${sessionScope.user.username}</c:if>">关注的人</a></span>
        <span href="#"><a class="followed" href="relationServlet?action=queryFollowed&username=<c:if test="${not empty sessionScope.queryUserName}">${sessionScope.queryUserName}</c:if><c:if test="${empty sessionScope.queryUserName}">${sessionScope.user.username}</c:if>">粉丝</a></span>

        <span href="#"><a class="followed" href="user/favourServlet?action=queryUserFavour&username=<c:if test="${not empty sessionScope.queryUserName}">${sessionScope.queryUserName}</c:if><c:if test="${empty sessionScope.queryUserName}">${sessionScope.user.username}</c:if>">获赞</a></span>
      </div>





    </div>
  </div>
  <!-- 右侧区域，显示博客列表 -->
  <div class="container-right">
      <c:if test="${not empty sessionScope.page.items}">
    <!-- 每个 blog 代表一篇博客 -->
    <c:forEach items="${sessionScope.page.items}" var="blogs">

    <div class="blog">
<%--      <div class="title">${blogs.count}</div>--%>
<%--      <div class="date">2022-2-16</div>--%>
<%--  style="width: 180px ;height: 200px; border: red solid 1px "--%>
      <div   class="desc">
        ${blogs.content}
      </div><br/>

<%--  如果是用户本人或者是管理员才能删除和修改文章  --%>
    <c:if test="${sessionScope.self==true || sessionScope.admin==true}">

<%--        <a class="deleteBlog"   id="deleteBlog"  href="blogServlet?action=deleteBlogByBlogId&blogId=${blogs.id}&username=${sessionScope.user.username}&pageNo=${sessionScope.page.pageNo}">删除文章</a>--%>
        <a class="deleteBlog"   id="deleteBlog"  href="blogServlet?action=deleteBlogByBlogId&blogId=${blogs.id}&username=${sessionScope.user.username}&pageNo=${sessionScope.page.pageNo}">删除文章</a>
        <a class="updateBlog"   id="updateBlog" href="blogServlet?action=updateBlog&blogId=${blogs.id}&pageNo=${sessionScope.page.pageNo}">修改文章</a>
    </c:if>
    <a class="showBlog" href="blogServlet?action=querySingleBlog&blogId=${blogs.id}" class="detail">查看全文&gt;&gt;</a>



    </div>

    </c:forEach>
      </c:if>

      <%--静态包含分页条--%>
      <%@include file="/pages/common/page_nav.jsp"%>

  </div>
</div>
</body>
</html>
